/**
 *
 * \file
 * iSendIR Main Code.
 *
 */

/*
 *
 * Copyright (c) 2010, Jesper Hansen
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *      * Redistributions of source code must retain the above copyright notice, this
 *       list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *     * Neither the name of the copyright holders nor the names of any contributors
 *       may be used to endorse or promote products derived from this software
 *       without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *
 */



#ifndef ISENDIR_H_
#define ISENDIR_H_


#include <stdint.h>


//!
//! iSendIR Firmware Version
//!
#define __FWVERSION__		"0.3"

//!
//! Time of compilation
//!
#define __FWCOMPILETIME__	__DATE__ "  " __TIME__


//! \name Timeout Flags
//@{
#define FLAG_BEACON		0x00000001	//!< Beacon
//@}

//! \name Miscellaneous Flags
//@{
#define FLAG_NETCONFIG	0x80000000	//!< Network config change
//@}



//*****************************************************************************
//! \name Timing Constants
// Defines for setting up the system clock.
// @{
//*****************************************************************************
#define SYSTICKHZ               1000					//!< Systick frequency
#define SYSTICKMS               (1000 / SYSTICKHZ)		//!< Miliseconds per systick
#define SYSTICKUS               (1000000 / SYSTICKHZ)	//!< Microseconds per systick
#define SYSTICKNS               (1000000000 / SYSTICKHZ)//!< Nanoseconds per systick
//@}


//*****************************************************************************
//! \name Interrupt priority definitions.
//! The top 3 bits of these values are significant with lower values
//! indicating higher priority interrupts.
// @{
//*****************************************************************************
#define SYSTICK_INT_PRIORITY    0x80
#define ETHERNET_INT_PRIORITY   0xC0
//@}


//*****************************************************************************
//!
//! Structure containing the network related settings.
//! Used when collecting data from the web-page or set_net command.
//!
//*****************************************************************************
typedef struct network_settings_t
{
	uint32_t ns_usedhcp;		//!< Indicator for DHCP mode. 0 = USE_STATIC, 1 = USE_DHCP
	uint32_t ns_ipaddr;			//!< IP address if using static addressing
	uint32_t ns_netmask;		//!< Network Mask if using static addressing
	uint32_t ns_gwaddr;			//!< Gateway address if using static addressing
	uint32_t ns_beacon_bpm;		//!< Beacon transmissions per minute.
} network_settings_t;

extern network_settings_t nw_settings;



#define NUM_IR_CHANNELS		3		//!< Number of IR channels

//*****************************************************************************
//!
//! Enumeration of possible channel modes
//!
//*****************************************************************************
typedef enum ir_channel_mode_e
{
	IRCM_OUT,					//!< Normal pulsed IR output on a carrier frequency
	IRCM_OUT_NOCARRIER,			//!< Pulsed output with no carrier frequency
	IRCM_IN,					//!< IR data input channel
	IRCM_IN_NOTIFY				//!< IR input channel with notification
} ir_channel_mode_e;


//*****************************************************************************
//!
//! Structure containing the channel modes for the IR inputs/outputs
//!
//*****************************************************************************
typedef struct infrared_settings_t
{
	ir_channel_mode_e is_channel_mode[NUM_IR_CHANNELS];	//!< Array of channel modes
} infrared_settings_t;

extern infrared_settings_t ir_settings;


//*****************************************************************************
//! \name SoftEEPROM Constants
// Defines for the SoftEEPROM area.
//@{
//*****************************************************************************
#define SOFTEEPROM_START	0x0003F000	//!< Start of SoftEEPROM location in flash
#define SOFTEEPROM_END		0x00040000	//!< End of SoftEEPROM location in flash
//@}


//
// Error Macro
// prints detailed error and location information
//
#define ERROR(...)	{	printf ("\nERROR (%s:%d): ", __FILE__, __LINE__); printf ( __VA_ARGS__ ); printf("\n"); }


char *ip2string(char *pbuffer, unsigned long ip);
char *mac2string(char *pbuffer, char *pformat);




//! \addtogroup iopins I/O pin usage
//! \code
/*
	Function	CPU Pin(s)			Description
	---------  	------------		------------
	#SDCS		PD0/IDX0			SD-card Chip Select, active low
	SSICLK		PA2/SSI0CLK			SD-card Clock
	SSIRX		PA4/SSI0RX			SD-card data in from card
	SSITX		PA4/SSI0TX			SD-card data out to card

	RXD0		PA0/U0RX			UART Receive for RS-232 I/O port
	TXD0		PA1/U0TX			UART Transmit for RS-232 I/O port
    RTS         PA6/I2C1SCL			UART RTS output for RS-232 I/O port
    CTS         PA7/I2C1SDA			UART CTS input for RS-232 I/O port

	RXD1		PD2/U1RX			Debug channel receive, connected to JTAG connector, pin 17
	TXD1		PD3/U1TX			Debug channel transmit, connected to JTAG connector, pin 19

	IR_IN_1		PD7/CCP1			IR input from channel 1, middle ring on 3.5mm jack
	IR_IN_2		PC6/CCP3			IR input from channel 2, middle ring on 3.5mm jack
	IR_IN_3		PC7/PHB0			IR input from channel 3, middle ring on 3.5mm jack

	IR_OUT_1	PF0/PWM0			IR output channel 1, active low, drives P-FET
	IR_OUT_2	PB0/PWM2			IR output channel 2, active low, drives P-FET
	IR_OUT_3	PE0/PWM4			IR output channel 3, active low, drives P-FET

	IO_1		PD4/CCP0, ADC2			Extra I/O connector pin 3
	IO_2		PD5/CCP2, ADC3			Extra I/O connector pin 4
	IO_3		PC4/PHA0, PG0/U2RX		Extra I/O connector pin 5
	IO_4		PC5/C1+/C10, PG1/U2TX	Extra I/O connector pin 6
	IO_PWR		PE3/PHA1			Power switch output for Extra I/O, active low, drives P-FET

	TP1			PB3/I2C0SDA			PCB testpoint 1
	TP2			PB2/I2C0SCL			PCB testpoint 2
	TP3			PE2/PHB1			PCB testpoint 3
	TP4			PE1/PWM5			PCB testpoint 4
	TP5			PB5/C1-				PCB testpoint 5
	TP6			PB6/C0+				PCB testpoint 6, connect to GND to enable bootloader.
	TP7			VCC					PCB testpoint 7
	TP8			GND					PCB testpoint 8
	TP9			PB4/C0-				PCB testpoint 9

	TCK			PC0/TCK/SWCLK		JTAG/SWD connector pin 9
	TMS			PC1/TMS/SWDIO		JTAG/SWD connector pin 7
	TDI			PC2/TDI				JTAG/SWD connector pin 5
	TDO			PC3/TDO/SWO			JTAG/SWD connector pin 13
	TRST		PB7/TRST			JTAG/SWD connector pin 3
	SRST		RST					JTAG/SWD connector pin 15
*/
//! \endcode



#endif /* ISENDIR_H_ */
